import Util from './util'
import { APP_FUN } from './enums'
/**
 * native2js
 * @param name
 * @param fn
 */
const _2js = (name, fn) => window[name] = fn
/**
 * js2native
 * @param name 
 * @param param 
 */
const _js2 = (name, param) => {
    param = param || ''
    param = Util.isString(param) ? param : JSON.stringify(param)
    try {
        // ios执行
        window.webkit.messageHandlers[name].postMessage(param)
    } catch (err) {
        // android执行
        try {
            window.JavaScriptInterface[name](param)
        } catch (e) {
            // 捕获不到执行
            console.warn("请在native环境下执行")
        }
    }
}

const mjk = {
    show: o => _js2(APP_FUN[1001], {
        ...o,
        fullScreen: o ? ( o.fullScreen ? o.fullScreen : 0 ): 0, //是否全屏幕
        appearRefresh: o ? (o.appearRefresh ? o.appearRefresh : 0) :0,//刷新页面
        goback: 1
    }), // 页面展示
    openUrl: url => _js2(APP_FUN[1002], {
        url
    }), // 打开新页面
    saveImage: o => _js2(APP_FUN[2001], o), // 保存图片
    getMiaoDeviceBindStatus: o => _js2(APP_FUN[3001], o), // 体脂秤链接状态
    goBack: o => _js2(APP_FUN[1003],{
        ...o,
        refresh:0
    }) // 返回上一页
}

export default {
    _2js,
    _js2,
    mjk
}